package com.dimples.dd.common.validator;

import com.dimples.dd.common.annotation.IsMobile;
import com.dimples.dd.common.util.validation.ValidationUtils;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

/**
 * 校验是否为合法的手机号码
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/6/21
 */
public class MobileValidator implements ConstraintValidator<IsMobile, String> {
    
    @Override
    public void initialize(IsMobile isMobile) {
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        try {
            // 如果手机号为空，默认不校验，即校验通过
            if (StringUtils.isBlank(s)) {
                return true;
            } else {
                // 校验手机
                return ValidationUtils.isMobile(s);
            }
        } catch (Exception e) {
            return false;
        }
    }
}
